#pragma once
#include "container.h"


#define CONSTRAINT_PARENT_LEFT      0x0100
#define CONSTRAINT_PARENT_RIGHT     0x0101
#define CONSTRAINT_PARENT_TOP       0x0102
#define CONSTRAINT_PARENT_BOTTOM    0x0103

#define CONSTRAINT_PARENT_WIDTH     0x0104
#define CONSTRAINT_PARENT_HEIGHT    0x0105

#define CONSTRAINT_PARENT_HCENTER     0x0106
#define CONSTRAINT_PARENT_VCENTER     0x0107
#define	CONSTRAINT_KEEP_WIDGET_EXTENT 0x0108

#define CONSTRAINT_PREV_LEFT        0x0200
#define CONSTRAINT_PREV_RIGHT       0x0201
#define CONSTRAINT_PREV_TOP         0x0202
#define CONSTRAINT_PREV_BOTTOM      0x0203

typedef struct  
{
   int type;
   int offset;
} ConstraintElem;

typedef struct 
{
   ConstraintElem left;
   ConstraintElem right;
   ConstraintElem top;
   ConstraintElem bottom;
} Constraint;

typedef struct 
{
   Constraint constraint;
   bool bVisible;
} WidgetConstraint;


static __inline bool Constraint_equals(const WidgetConstraint *c1, const WidgetConstraint *c2) {
#define ELEM_EQUALS(e1,e2) (e1.type == e2.type && e1.offset == e2.offset)

   return ELEM_EQUALS(c1->constraint.left, c2->constraint.left)
      && ELEM_EQUALS(c1->constraint.right, c2->constraint.right)
      && ELEM_EQUALS(c1->constraint.top, c2->constraint.top)
      && ELEM_EQUALS(c1->constraint.bottom, c2->constraint.bottom)
	  && (c1->bVisible == c2->bVisible);
}
      


class DLL_EXP CConstraintContainer :
	public CContainerWidget
{
public:
	CConstraintContainer(void);
	virtual ~CConstraintContainer(void);
public:
	int SetConstraint(CWidget* pWidget, const WidgetConstraint* pWC, CWidget* pWidgetBefore);
	virtual int DoLayout(CWidget* pWidget,WRect* pInvRect=NULL);
	int LayoutWidget(CWidget* pWidget);
	virtual int ExtendBoundary(WRect* wn);
};
